package com.baijiayun.liveuibase.widgets.dialog

import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.widget.AppCompatCheckBox
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.baijiayun.liveuibase.R
import com.baijiayun.liveuibase.utils.ThemeDataUtil
import com.baijiayun.liveuibase.utils.ToastUtil
import com.baijiayun.liveuibase.utils.drawable.DrawableBuilder
import kotlinx.android.synthetic.main.bjy_base_report_dialog.*
import kotlinx.android.synthetic.main.uibase_base_title_dialog.*

/**
 * @author      yongjiaming
 * @date        2021/9/28
 * @description 聊天举报dialog
 */
class ReportDialog(context: Context) : BaseTitleDialog(context) {

    val reportArray: Array<String> by lazy {
        context.resources.getStringArray(R.array.bjy_base_report_array)
    }

    var submitListener: IReportSubmitListener? = null

    val reportReasonList = mutableListOf<String>()

    override fun getContentLayout(): Int {
        return R.layout.bjy_base_report_dialog
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window?.run {
            val params = attributes
            params.width = context.resources.getDimension(R.dimen.bjy_base_live_report_dialog_width).toInt()
            params.height = context.resources.getDimension(R.dimen.bjy_base_live_report_dialog_height).toInt()
            attributes = params
            setGravity(Gravity.CENTER)
        }
        initView()
    }

    private fun initView() {
        base_dialog_title_text.text = context.getString(R.string.bjy_base_report_reason)
        report_rv.layoutManager = LinearLayoutManager(context)
        report_rv.adapter = ReportAdapter()

        val backgroundDrawable = DrawableBuilder()
            .solidColor(ThemeDataUtil.getColorFromThemeConfigByAttrId(context, R.attr.base_theme_live_product_color))
            .cornerRadius(context.resources.getDimensionPixelSize(R.dimen.bjy_base_common_bg_radius))
            .build()
        report_btn.background = backgroundDrawable
        report_btn.setOnClickListener {
            if (reportReasonList.isEmpty()) {
                ToastUtil(context).setText(context.getString(R.string.bjy_base_report_reason)).create().show()
            } else {
                submitListener?.onSubmit(reportReasonList)
            }
        }
    }

    inner class ReportAdapter : RecyclerView.Adapter<ReportViewHolder>() {
        override fun onBindViewHolder(holder: ReportViewHolder, position: Int) {
            holder.textView.text = reportArray[position]
            holder.checkBox.setOnCheckedChangeListener { buttonView, isChecked ->
                if (isChecked) {
                    if (!reportReasonList.contains(reportArray[position])) {
                        reportReasonList.add(reportArray[position])
                    }
                } else {
                    reportReasonList.remove(reportArray[position])
                }
            }
        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ReportViewHolder {
            val itemView = LayoutInflater.from(context).inflate(R.layout.bjy_base_report_item, parent, false)
            return ReportViewHolder(itemView)
        }

        override fun getItemCount(): Int {
            return reportArray.size
        }
    }

    class ReportViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var checkBox: AppCompatCheckBox
        var textView: TextView

        init {
            checkBox = itemView.findViewById(R.id.report_item_checkbox)
            textView = itemView.findViewById(R.id.report_item_reason)
        }
    }

    interface IReportSubmitListener {
        fun onSubmit(reasonList: MutableList<String>)
    }
}
